package com.foreveredu.propms.web.struts.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.service.IPropQueryService;
import com.foreveredu.propms.web.struts.actionform.DistMonthlyQueryActionForm;
import com.foreveredu.propms.web.util.ExportExcelUtils;

/**
 * 处理物业服务处收费查询请求
 * @author caohuayang
 *   
 */
public class DistMonthlyQueryAction extends AbstractBaseDispatchAction {
	
	private IManagementService managementService = null;
	private IPropQueryService propQueryService = null;
	
	//定义接受查询结果的成员变量
	private List<Map<String, Object>> sumQueryList;
	//查询条件
	private int year;     //查询的年份
	private int month; //查询的月份
	
	// 获得日志记录器
	Logger log = Logger.getLogger(AccountAction.class);  

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IPropQueryService getPropQueryService() {
		return propQueryService;
	}

	public void setPropQueryService(IPropQueryService propQueryService) {
		this.propQueryService = propQueryService;
	}

	// 跳转到物业服务处收费月汇总查询条件页面
	public ActionForward queryDistMonthlyCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得所有的物业服务处
		List<District> distList = managementService.findDistricts();
		request.setAttribute("distList", distList);
		return mapping.findForward("query_distMonthlyCharge");
	}
	
	//执行服务处收费月汇总查询操作
	public ActionForward doQueryDistMonthlyCharge (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//获得表单数据
		DistMonthlyQueryActionForm dmqform = (DistMonthlyQueryActionForm) form;
		int distId = dmqform.getDistId();
		year = dmqform.getYear();
	    month = dmqform.getMonth();
		
		sumQueryList = new ArrayList<Map<String,Object>>();
		//当管理员不选择物业服务处，则查询所有物业服务处当月物业费
		if(distId == 0){
			//获得所有的物业服务区
			List<District> distList = managementService.findDistricts();
			for(District dist:distList){
				sumQueryList.add(propQueryService.queryDistMonthlyCharge(dist, year, month));
			}	
		}else{
			//根据distId来查询业务服务区的信息
			District district = managementService.findDistrictById(distId);
			//根据业务服务处ID来查询下面所有小区的当月物业费
			sumQueryList.add(propQueryService.queryDistMonthlyCharge(district, year, month));
		}
		request.setAttribute("sumQueryList", sumQueryList);
		request.setAttribute("year", year);
		request.setAttribute("month", month);
		
		return mapping.findForward("doQuery_distMonthlyCharge");
	}
	
	//将数据生成到Excel表格中并导出
	public ActionForward doExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			response.setContentType("octets/stream"); 
			//设置表名
			String fileName="物业服务处收费月汇总("+year+"年"+month+"月)";
			response.addHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859-1")+".xls"); 
			
            OutputStream out = response.getOutputStream();
            
            //设置表格中的字段名
            String[] headers = {"服务处", "应缴金额", "实收金额","物业服务费","电费","水费","停车服务费","垃圾清运费","路灯电费","二次提水费"};
            //设置Key，然后通过这个Key去Map数组中取值
            String[] columns ={"distName","totalSum","paidSum","propCharge","powerCharge","waterCharge","carCharge","rubbishCharge","lightCharge","water2ndCharge"};
            
            ExportExcelUtils.exportExcel("物业服务处月汇总表", headers, columns, sumQueryList, out, "");
            out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}




















